//
//  IndexViewController.swift
//  downloadImage
//
//  Created by Galaxy on 2022/6/20.
//

import UIKit
import SnapKit
import WCDBSwift
class IndexViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        print(FileTool.init().getDocumentPath())
        insertDb()
        findData()
    }
    
    func insertDb() {
        // 创建数据表
        try! FileTool().getDb().create(table: "article", of: ArticleModel.self)
        try! FileTool().getDb().create(table: "image", of: ImageModel.self)
    }
    
    lazy var mainTable: IndexListTableView = {
        let tableView = IndexListTableView.init(frame: .zero, style:.insetGrouped)
        view.addSubview(tableView)
        tableView.cellItemDidSelectedBlock = { indexPath in
            if indexPath.section == tableView.listArr.count-1 {
                if indexPath.row == 0 {
                    // 下载图片
                    let VC = DownloadImgViewController.init()
                    self.navigationController?.pushViewController(VC, animated: true)
                }else{
                    // 删除本地log文件
                    LogTool.init().deleteLog()
                }
            }else{
                // 爬取数据
                let VC = DownloadViewController.init()
                VC.webSite = indexArr[indexPath.section][indexPath.row]
                self.navigationController?.pushViewController(VC, animated: true)
            }
        }
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        return tableView
    }()
    
    // 查找数据
    func findData(){
        mainTable.listArr = indexArr
        mainTable.reloadData()
    }
}
